﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Entity;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using EKAABSInformationManagement.Service;
using EKAABSInformationManagement.Models;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Controllers
{
    public class NetworkController : Controller
    {
        public NetworkService myNetwork { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (System.Web.HttpContext.Current.User.IsInRole("admin") != true)
            {
                HttpException ex = new HttpException(401, "");
                requestContext.HttpContext.AddError(ex);
            }
            if (myNetwork == null) { myNetwork = new NetworkService(); }
            base.Initialize(requestContext);
        }
        public ActionResult Index()
        {
            List<networkInfo> network = myNetwork.getAllNetworkInfo();
            return View(network);
        }
        [HttpGet]
        public ActionResult getALLOverduedInfo()
        {
            List<network> network = myNetwork.getAllOverduedInfo();
            return View(network);
        }
        public ActionResult submit(network n)
        {
            try
            {
                bool isExists = default(bool);
                myNetwork.insert(n, out isExists);
                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpPost]
        public ActionResult update(networkInfo n)
        {
            try
            {
                myNetwork.update(n);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpPost]
        public ActionResult batchUpdate(string AliasList, networkInfo n)
        {
            try
            {
                string[] strs = AliasList.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                myNetwork.bacthUpdate(strs, n);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpPost]
        public ActionResult delete(string ids)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                myNetwork.delete(strs);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }

    }
}
